//
//  WYCAddBankController.m
//  信用卡管理
//
//  Created by 中企互联 on 2017/11/22.
//  Copyright © 2017年 李胜军. All rights reserved.
//首页里面的添加信用卡

#import "WYCAddCreditController.h"
#import "WYCRepaymentController.h"
#import "WYCAddPhotoController.h"
#import "WYCDetailCardController.h"
#import "WYCAddCreditController.h"
#import "WYCAddDebitbankController.h"

@interface WYCAddCreditController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    UIImagePickerController *_imagePickerController;

}
/** 正面按钮*/
@property(nonatomic,strong)UIButton * cardButton;

/** 反面按钮*/
@property(nonatomic,strong)UIButton * cardsideButton;

/** 反面按钮*/
@property(nonatomic,strong)UIButton * holdButton;
@end

@implementation WYCAddCreditController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //Nav按钮及颜色
    [self setupNavButton];
    
  
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cardtest) name:@"card" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cardsidetest) name:@"bankside" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(holdsidetest) name:@"holdBank" object:nil];
    
    //界面布局
    [self setupcreat];
}

- (void)holdsidetest{
    [_holdButton setTitle:@"已认证" forState:UIControlStateNormal];
    [_holdButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [_holdButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    _holdButton.enabled = NO;
    
}
- (void)cardsidetest{
    [_cardsideButton setTitle:@"已认证" forState:UIControlStateNormal];
    [_cardsideButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [_cardsideButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    _cardsideButton.enabled = NO;
}
- (void)cardtest{
    [_cardButton setTitle:@"已认证" forState:UIControlStateNormal];
    [_cardButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [_cardButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    _cardButton.enabled = NO;
}
//界面布局
- (void)setupcreat{
    UIView * backView = [[UIView alloc] init];
    backView.frame = CGRectMake(0, NavHeight, ScreenWidth, 460 * px);
    backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView];
    
    //信用卡正面
    UILabel * PositiveLabel = [[UILabel alloc] init];
    PositiveLabel.frame = CGRectMake(40 * px, 40 * px, 300 * px, 72 * px);
    PositiveLabel.font = [UIFont systemFontOfSize:MiddleFont];
    PositiveLabel.text = @"信用卡正面";
    [backView addSubview:PositiveLabel];
    
    //分割线
    UIView * pline = [[UIView alloc] init];
    pline.frame = CGRectMake(40 * px, CGRectGetMaxY(PositiveLabel.frame) + 40 * px, ScreenWidth, 1);
    pline.backgroundColor = WYCRGBColor(242, 242, 242);
    [backView addSubview:pline];
    
    
    //是否扫描
    UIButton * cardButton = [[UIButton alloc] init];
    cardButton.frame = CGRectMake(backView.width  - 180 * px, PositiveLabel.y, 100 * px, PositiveLabel.height);
    cardButton.titleLabel.font = [UIFont systemFontOfSize:30 * px];
    [cardButton setImage:[UIImage imageNamed:@"scanning"] forState:UIControlStateNormal];
    [cardButton addTarget:self action:@selector(cardButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:cardButton];
    self.cardButton = cardButton;
    
    
    
    
    
    /**********************
     *********信用卡反面*****
     **********************/
    
    //信用卡反面
    UILabel * sideLabel = [[UILabel alloc] init];
    sideLabel.frame = CGRectMake(40 * px, CGRectGetMaxY(pline.frame) + 40 * px, 300 * px, 72 * px);
    sideLabel.font = [UIFont systemFontOfSize:MiddleFont];
    sideLabel.text = @"信用卡反面";
    [backView addSubview:sideLabel];
    
    //分割线
    UIView * sline = [[UIView alloc] init];
    sline.frame = CGRectMake(40 * px, CGRectGetMaxY(sideLabel.frame) + 40 * px, ScreenWidth, 1);
    sline.backgroundColor = WYCRGBColor(242, 242, 242);
    [backView addSubview:sline];
    
    
    //是否扫描
    UIButton * cardsideButton = [[UIButton alloc] init];
    cardsideButton.frame = CGRectMake(backView.width  - 180 * px, sideLabel.y, 100 * px, PositiveLabel.height);
    cardsideButton.titleLabel.font = [UIFont systemFontOfSize:30 * px];
    [cardsideButton setImage:[UIImage imageNamed:@"scanning"] forState:UIControlStateNormal];
    [cardsideButton addTarget:self action:@selector(cardsideButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:cardsideButton];
    self.cardsideButton = cardsideButton;
    
    
    
    /**********************
     *****手持信用卡正面*****
     **********************/
    
    //信用卡反面
    UILabel * holdLabel = [[UILabel alloc] init];
    holdLabel.frame = CGRectMake(40 * px, CGRectGetMaxY(sline.frame) + 40 * px, 500 * px, 72 * px);
    holdLabel.font = [UIFont systemFontOfSize:MiddleFont];
    holdLabel.text = @"手持信用卡正面";
    [backView addSubview:holdLabel];
    
    //分割线
    UIView * hline = [[UIView alloc] init];
    hline.frame = CGRectMake(40 * px, CGRectGetMaxY(holdLabel.frame) + 40 * px, ScreenWidth, 1);
    hline.backgroundColor = WYCRGBColor(242, 242, 242);
    [backView addSubview:hline];
    
    //手持信用卡
    UIButton * holdcardButton = [[UIButton alloc] init];
    holdcardButton.frame = CGRectMake(backView.width  - 180 * px, holdLabel.y, 100 * px, PositiveLabel.height);
    [holdcardButton setImage:[UIImage imageNamed:@"Photograph"] forState:UIControlStateNormal];;
    holdcardButton.titleLabel.font = [UIFont systemFontOfSize:30 * px];
    [holdcardButton addTarget:self action:@selector(holdcardButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:holdcardButton];
    self.holdButton = holdcardButton;
    
    
    
    
    //提示
    UILabel * messege = [[UILabel alloc] init];
    messege.frame = CGRectMake(40 * px, CGRectGetMaxY(backView.frame) + 80 * px, ScreenWidth - 80 * px, PositiveLabel.height);
    messege.textAlignment = NSTextAlignmentCenter;
    messege.textColor = WYCRGBColor(123, 123, 238);
    messege.font = [UIFont systemFontOfSize:SmallFont];
    messege.text = @"手持信用卡请完全漏出手臂，确保信用卡号码等信息清晰可见";
    [self.view addSubview:messege];
    
    
    //提交按钮
    UIButton *SubmitButton = [[UIButton alloc] init];
    SubmitButton.frame = CGRectMake(40 * px, CGRectGetMaxY(messege.frame) + 160 * px, ScreenWidth - 80 * px, 160 * px);
    SubmitButton.layer.cornerRadius = 5;
    SubmitButton.backgroundColor = wycMain;
    [SubmitButton setTitle:@"提交" forState:UIControlStateNormal];
    [SubmitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [SubmitButton addTarget:self action:@selector(submitButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:SubmitButton];
}



//Nav按钮及颜色
- (void)setupNavButton{
    self.navigationItem.title = @"添加信用卡";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [self.navigationController.navigationBar setBarTintColor:wycMain];
}


- (void)submitButtonClick{
    if (![_cardButton.titleLabel.text isEqualToString:@"已认证"]) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"请扫描银行卡正面"];
        return;
    }else if (![_cardsideButton.titleLabel.text isEqualToString:@"已认证"]) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"请扫描银行卡反面"];
        return;
    }else if (![_holdButton.titleLabel.text isEqualToString:@"已认证"]) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"请上传手持银行卡照片"];
        return;
    }else{
        WYCDetailCardController * DetailCard= [[WYCDetailCardController alloc]init];
        DetailCard.type = self.strtepy;
        [self.navigationController pushViewController:DetailCard animated:NO];
    }
}





#pragma make --------->>正面
- (void)cardButtonClick{
    WYCAddPhotoController * showimage = [[WYCAddPhotoController alloc] init];
    showimage.strType = @"1";
    [self.navigationController pushViewController:showimage animated:NO];
    
}

#pragma make --------->>反面
- (void)cardsideButtonClick{
    
    WYCAddPhotoController * showimage = [[WYCAddPhotoController alloc] init];
    showimage.strType = @"2";
    [self.navigationController pushViewController:showimage animated:NO];
}

#pragma make --------->>手持
- (void)holdcardButtonClick{
    
    [self loadCamer];
//    WYCAddPhotoController * showimage = [[WYCAddPhotoController alloc] init];
//    showimage.strType = @"3";
//    [self.navigationController pushViewController:showimage animated:NO];
}
-(void)loadCamer
{
    UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.editing = YES;
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;
    //选择相机时，设置UIImagePickerController对象相关属性
    imagePicker.sourceType =  UIImagePickerControllerSourceTypeCamera;
    imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
    imagePicker.mediaTypes = @[(NSString *)kUTTypeImage];
    imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    //跳转到UIImagePickerController控制器弹出相机
    [self presentViewController:imagePicker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo {
    
    [SVProgressHUD showWithStatus:@"加载中...."];
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    
    NSData * data = UIImageJPEGRepresentation(image, 1);
    NSString *encodedImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    params[@"base64Img"] = encodedImageStr;
    [HttpTool POST:uploadimage params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            
            NSString *bankCardUrl = [responseObject objectForKey:@"result"];
            
            NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documents = [array lastObject];
            NSString *documnetPath = [documents stringByAppendingPathComponent:@"holdBank.plist"];
            //准备要存入的字典
            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:bankCardUrl,@"bankCardUrl",nil];
            //将字典存入指定的本地文件
            [dic writeToFile:documnetPath atomically:YES];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"holdBank" object:nil];
            NSArray *temArray = self.navigationController.viewControllers;
            for(UIViewController *temVC in temArray)
                
            {
                
                if ([temVC isKindOfClass:[WYCAddCreditController class]])
                    
                {
                    
                    [self.navigationController popToViewController:temVC animated:YES];
                    
                }
                [SVProgressHUD dismiss];
                
            }
            
        }else{
            [SVProgressHUD dismiss];
            
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD dismiss];
        
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
    }];
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
    [self dismissViewControllerAnimated:NO completion:nil];
    
}
-(void)viewWillAppear:(BOOL)animated
{
  
    self.navigationController.navigationBarHidden =NO;
}
-(void)viewWillDisappear:(BOOL)animated
{
   
    self.navigationController.navigationBarHidden =NO;

}

@end

